<?php 
namespace api\home\controller;
use cmf\controller\RestBaseController;
use think\Db;
/**
* 医生聊天数据
*/
class MessigeController extends RestBaseController{
	public function __construct(){
		$this->data = input();
	}
	//聊天数据列表
	public function list(){
		if (!isset($this->data['openid'])||empty($this->data['openid'])) {
			$this->error('缺少参数[openid]');
		}
		if (!isset($this->data['chatid'])||empty($this->data['chatid'])) {
			$this->error('缺少参数[chatid]');
		}
		$list = Db::table('messagerecord')->field('a.*,b.nickname as userName,b.headimgurl as userHeadImg,c.name as doctorName,c.headimg as doctorHeadImg')->alias('a')->prefix_close()->
			join('miniuser b','b.openid = a.replyopenid','LEFT')->
			join('doctor c','a.doctorid = c.id','LEFT')->
			where([
					'a.openid'=>$this->data['openid'],
					'a.chatid'=>$this->data['chatid']
				])->order('a.id')->select();
		$this->success('success',$list);
	}
	//医生回复信息
	public function huifu1(){
		if (!isset($this->data['openid'])||empty($this->data['openid'])) {
			$this->error('缺少参数[openid]');
		}
		if (!isset($this->data['biz_id'])||empty($this->data['biz_id'])) {
			$this->error('缺少参数[biz_id]');
		}
		if (!isset($this->data['chatid'])||empty($this->data['chatid'])) {
			$this->error('缺少参数[chatid]');
		}
		if (!isset($this->data['doctorid'])||empty($this->data['doctorid'])) {
			$this->error('缺少参数[doctorid]');
		}
		if (!isset($this->data['messagetype'])||empty($this->data['messagetype'])) {
			$this->error('缺少参数[messagetype]，1文本 2图片');
		}
		if (!isset($this->data['content'])||empty($this->data['content'])) {
			$this->error('内容不能为空！[content]');
		}
		$data['replyopenid'] = $this->data['openid'];
		$data['bizid'] = $this->data['biz_id'];
		$data['openid'] = $this->data['openid'];
		$data['doctorid'] = $this->data['doctorid'];
		$data['messagetype'] = $this->data['messagetype'];
		$data['chatid'] = $this->data['chatid'];
		$data['content'] = $this->data['content'];
		$data['createtime'] = date('Y-m-d H:i:s',time());
		$data['updatetime'] = date('Y-m-d H:i:s',time());
		$data['usertype'] = 1; //1医生 2助手 3用户 
		Db::table('messagerecord')->insert($data);
		$this->success('success');
	}
	//助手回复信息
	public function huifu2(){
		if (!isset($this->data['openid'])||empty($this->data['openid'])) {
			$this->error('缺少参数[openid]');
		}
		if (!isset($this->data['biz_id'])||empty($this->data['biz_id'])) {
			$this->error('缺少参数[biz_id]');
		}
		if (!isset($this->data['chatid'])||empty($this->data['chatid'])) {
			$this->error('缺少参数[chatid]');
		}
		if (!isset($this->data['doctorid'])||empty($this->data['doctorid'])) {
			$this->error('缺少参数[doctorid]');
		}
		if (!isset($this->data['messagetype'])||empty($this->data['messagetype'])) {
			$this->error('缺少参数[messagetype]，1文本 2图片');
		}
		if (!isset($this->data['content'])||empty($this->data['content'])) {
			$this->error('内容不能为空！[content]');
		}
		$data['replyopenid'] = $this->data['openid'];
		$data['bizid'] = $this->data['biz_id'];
		$data['openid'] = $this->data['openid'];
		$data['doctorid'] = $this->data['doctorid'];
		$data['messagetype'] = $this->data['messagetype'];
		$data['chatid'] = $this->data['chatid'];
		$data['content'] = $this->data['content'];
		$data['createtime'] = date('Y-m-d H:i:s',time());
		$data['updatetime'] = date('Y-m-d H:i:s',time());
		$data['usertype'] = 2; //1医生 2助手 3用户 
		Db::table('messagerecord')->insert($data);
		$this->success('success');
	}
	//用户回复信息
	public function huifu3(){
		if (!isset($this->data['openid'])||empty($this->data['openid'])) {
			$this->error('缺少参数[openid]');
		}
		if (!isset($this->data['biz_id'])||empty($this->data['biz_id'])) {
			$this->error('缺少参数[biz_id]');
		}
		if (!isset($this->data['replyopenid'])||empty($this->data['replyopenid'])) {
			$this->error('缺少参数[replyopenid]');
		}
		if (!isset($this->data['chatid'])||empty($this->data['chatid'])) {
			$this->error('缺少参数[chatid]');
		}
		if (!isset($this->data['doctorid'])||empty($this->data['doctorid'])) {
			$this->error('缺少参数[doctorid]');
		}
		if (!isset($this->data['messagetype'])||empty($this->data['messagetype'])) {
			$this->error('缺少参数[messagetype]，1文本 2图片');
		}
		if (!isset($this->data['content'])||empty($this->data['content'])) {
			$this->error('内容不能为空！[content]');
		}
		$data['replyopenid'] = $this->data['replyopenid'];
		$data['bizid'] = $this->data['biz_id'];
		$data['openid'] = $this->data['openid'];
		$data['doctorid'] = $this->data['doctorid'];
		$data['messagetype'] = $this->data['messagetype'];
		$data['chatid'] = $this->data['chatid'];
		$data['content'] = $this->data['content'];
		$data['createtime'] = date('Y-m-d H:i:s',time());
		$data['updatetime'] = date('Y-m-d H:i:s',time());
		$data['usertype'] = 3; //1医生 2助手 3用户 
		Db::table('messagerecord')->insert($data);
		$this->success('success');
	}
	//聊天图片上传
	public function megimg(){
		$file = request()->file('image');
		if($file){
	        $info = $file->move(ROOT_PATH . 'public' . DS . 'upload' . DS . 'message');
	        if($info){
	            if( !in_array($info->getExtension(), ['png','jpg'])){
	            	$this->error('图片上传失败，只能上传jpg、png格式图片');
	            }
	            $this->success('上传成功！',Config::get('weburl').'upload'. DS .$info->getSaveName());
	        }else{
	           	$this->error('没有检测到上传的图片!');
	        }
	    }
	}
}